package com.nwpu.coursesupport.service.imple;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.nwpu.coursesupport.criteria.TeacherSearchCriteria;
import com.nwpu.coursesupport.entity.Teacher;
import com.nwpu.coursesupport.entity.bean.JSONResult;
import com.nwpu.coursesupport.mapper.TeacherMapper;
import com.nwpu.coursesupport.service.param.IDParam;
import com.nwpu.coursesupport.service.param.RegisterParam;
import com.nwpu.coursesupport.tool.EncryptionUtil;
import com.nwpu.coursesupport.tool.StringUtil;
import com.nwpu.coursesupport.tool.UpdateUtil;
/**
 * @author archerkang
 */
@Service
public class TeacherService extends UserService{
	
    @Autowired
    private TeacherMapper teacherMapper;

    public JSONResult detail(IDParam p) {
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null || p.getId() <= 0){
            rst.fail("参数有误");
            return rst;
        }
        
        Teacher teacher = this.teacherMapper.findById(p.getId());
        if(teacher==null){
        	return rst.fail("该用户不存在");
        }
        RegisterParam dao=new RegisterParam();
        UpdateUtil.setValues(dao, teacher);
        dao.setUserCode(teacher.getTeacherCode());
        rst.addData("user", dao);
        rst.success();
        return rst;
    }

    public JSONResult create(RegisterParam p) {
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null){
            return rst.fail("参数有误");
        }
        TeacherSearchCriteria sc=new TeacherSearchCriteria();
        sc.setTeacherCode(p.getUserCode());
        List<Teacher> list=this.teacherMapper.find(sc);
        if(list!=null&&list.size()>0){
        	return rst.fail("该用户已存在");
        }
        
        Teacher teacher = new Teacher();
        UpdateUtil.setValues(teacher, p);
		teacher.setTeacherCode(p.getUserCode());
		teacher.setPassword(EncryptionUtil.Encryption(teacher.getPassword(),teacher.getTeacherCode()));
		if(StringUtil.isEmpty(teacher.getPicture())){
			teacher.setPicture(getHeaderImage(teacher.getGender()));
        }
		Date date=new Date();
        teacher.setCreateDate(date);
        teacher.setUpdateDate(date);
        if (!this.teacherMapper.save(teacher)){
            return rst.fail("保存失败");
        }
        rst.addData("teacher", teacher);
        rst.success();
        return rst;
    }

    public JSONResult updateAvatar(RegisterParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null ||p.getPicture()==null){
            return rst.fail("参数有误");
        }
        
        Integer id=(Integer) getSession().getAttribute("id");
        Teacher teacher = this.teacherMapper.findById(id);
        if (teacher == null){
            return rst.fail("记录不存在或已被删除");
        }
        teacher.setPicture(p.getPicture());
        if(!this.teacherMapper.update(teacher)){
        	return rst.fail("用户修改失败");
        }
        getSession().setAttribute("image",teacher.getPicture());
        return rst.success();
    }
    
    public JSONResult update(RegisterParam p) throws Exception{
        JSONResult rst = new JSONResult();
        rst.fail();
        
        if (p == null ){
            return rst.fail("参数有误");
        }
        
        Integer id=(Integer) getSession().getAttribute("id");
        p.setId(id);
        Teacher teacher = this.teacherMapper.findById(p.getId());
        if (teacher == null){
            return rst.fail("记录不存在或已被删除");
        }
        
        if (UpdateUtil.setValues(teacher, p) > 0){
        	teacher.setUpdateDate(new Date());
            if (!this.teacherMapper.update(teacher)){
                return rst.fail("保存失败");
            }
        }
      
        RegisterParam dao=new RegisterParam();
        UpdateUtil.setValues(dao, teacher);
        dao.setUserCode(teacher.getTeacherCode());
        rst.addData("user", dao);
        rst.success();
        return rst;
    }

   /* public JSONResult changePass(StudentServiceParam.ChangePassParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if(p==null){
        	return rst.fail("参数有误");
        }
        p.setId((Integer)getSession().getAttribute("id"));
        p.setStudentCode((String)getSession().getAttribute("studentCode"));
        Student student=this.studentMapper.findByStudentCode(p.getStudentCode());
        if(!student.getPassword().equals(p.getOldPassword())){
        	return rst.fail("旧密码错误");
        }
        p.setUpdateDate(new Date());
        if(!this.studentMapper.changePass(p)){
        	return rst.fail("密码修改失败");
        }

        rst.success();
        return rst;
        
    }*/
    
    public Teacher findByTeacherCode(String teacherCode){
    	Teacher teacher = this.teacherMapper.findByTeacherCode(teacherCode);
    	return teacher;
    }
    
    
    /**
     * 初版忘记密码这儿简单处理
     * 用户验证其注册所留Email
     * 	若通过，允许修改
     * 	否则，不允许修改
     * @param p
     * @return
     *//*
    public JSONResult forgetPass(ChangePassParam p){
    	JSONResult rst = new JSONResult();
        rst.fail();
        
        if(p==null||p.getStudentCode() == null){
        	return rst.fail("参数有误");
        }
        
        Student student=this.studentMapper.findByStudentCode(p.getStudentCode());
        if(student == null){
        	return rst.fail("该用户不存在");
        }
        
        if(student.getEmail().equals(p.getEmail())){
        	return rst.fail("你所输入的email与注册所留不同");
        }
        
        p.setUpdateDate(new Date());
        p.setId(student.getId());
        if(!this.studentMapper.changePass(p)){
        	return rst.fail("密码修改失败");
        }

        rst.success();
        return rst;
    }*/
   
}
